//
//  ByBetRecordVC.m
//  caipiao_ios
//
//  Created by mc on 2018/8/6.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByBetRecordVC.h"
#import "ByCoreViewSegmentControl.h"
#import "ByBetRecordCell.h"
#import "ByBetRecordDetailVC.h"
#import "ByBallBetDetailVC.h"
#import "BySuperLottoVC.h"
#import "ByDoubleBallVC.h"
#import "ByFootBallBetVC.h"
#import "ByBasketBallBetVC.h"

@interface ByBetRecordVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) ByCoreViewSegmentControl *segTitle;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) NSInteger type;

@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) ByPayBombView *payBombView;
@property (nonatomic, strong) ByNetModel *orderModel;

@end

@implementation ByBetRecordVC

- (NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray arrayWithCapacity:1];
    }
    return _dataArray;
}


- (ByPayBombView *)payBombView{
    if (_payBombView == nil) {
        _payBombView = [[ByPayBombView alloc] init];
        
        __weak typeof(self) weakSelf = self;
        _payBombView.sureBlock = ^(){
            
            [weakSelf.payBombView remove];
            weakSelf.payBombView = nil;
            [weakSelf loadData];
        };
        _payBombView.otherSureBlock = ^(NSString *str,NSInteger type){
            
            [weakSelf.payBombView remove];
            weakSelf.payBombView = nil;
            
            if (type == 1) {
                ByWebViewController *vc = [[ByWebViewController alloc] initWithTotalUrlString:str];
                vc.titleString = @"支付宝支付";
                [weakSelf by_normalPush:vc hasNavi:YES];
            }else{
                ByWechatPayQrView *vc = [[ByWechatPayQrView alloc] init];
                vc.title = @"微信支付";
                vc.str = str;
                [weakSelf by_normalPush:vc hasNavi:YES];
            }
            
        };
        
        
        _payBombView.selectBlock = ^(NSInteger index){
            
        };
    }
    return _payBombView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addLeftBackBarButtonItem];
    self.title = @"投注记录";
    [self initViews];
    [self loadData];
    
    self.type = 0;
    ByCoreRefreshGifHeader *header = [ByCoreRefreshGifHeader headerWithRefreshingBlock:^{
        [self loadData];
    }];
    self.tableView.mj_header = header;
    // Do any additional setup after loading the view.
}

- (void)loadData{
    ByNetModel *netModel = [[ByNetModel alloc] init];
    netModel.key = [NSString stringWithFormat:@"%ld",self.type];
    [ByCoreToolAlert showLoadind:@"加载中" hasMasker:YES];
    [ByApiOther betRecoreList:netModel success:^(ByNetBaseResult *result) {
        [self.tableView.mj_header endRefreshing];
        [ByCoreToolAlert hideHud];
        SLog(@"投注记录 = %@", result.data);
        [self.dataArray removeAllObjects];
        for (NSDictionary *dic in result.data[@"data"]) {
            ByNetModel *netModel = [[ByNetModel alloc] initContentWithDic:dic];
            [self.dataArray addObject:netModel];
        }
        [self.tableView reloadData];
    } failure:^(ByNetBaseResult *result) {
        [self.tableView.mj_header endRefreshing];
        [ByCoreToolAlert showFailureWithText:result.msg];
        [self.dataArray removeAllObjects];
        [self.tableView reloadData];
    }];
}


-(void)initViews{
    self.segTitle = [[ByCoreViewSegmentControl alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 39)];
    [self.view addSubview:self.segTitle];
    [self _initData];
    
    self.view.backgroundColor = BY_BACKGROUND_COLOR;
    self.tableView = [[UITableView alloc] init];
    self.tableView.backgroundColor = BY_BACKGROUND_COLOR;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
       
        if (@available(ios 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(39);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(0);
        } else {
            make.top.equalTo(self.view).offset(39);
            make.bottom.equalTo(self.view).offset(0);
        }
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

#pragma mark -- 初始化数据 --
- (void)_initData{
    self.segTitle.sectionTitles = @[@"全部",@"中奖",@"未中奖",@"未成功"];
    self.segTitle.backgroundColor = [UIColor whiteColor];
    self.segTitle.font = FONT(14);
    self.segTitle.scrollEnabled = YES;
    self.segTitle.textColor = [UIColor blackColor];
    self.segTitle.selectedTextColor = BY_THEME_COLOR;
    self.segTitle.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
    self.segTitle.selectionStyle = HMSegmentedControlSelectionStyleTextWidthStripe;
    self.segTitle.selectionIndicatorHeight = 3;
    self.segTitle.segmentEdgeInset = UIEdgeInsetsMake(0, 0, 0, 0);
    __weak typeof(self) weakSelf = self;
    [self.segTitle setIndexChangeBlock:^(NSInteger index) {
        weakSelf.type = index;
        [weakSelf loadData];
    }];
    [self.view addSubview:self.segTitle];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ByBetRecordCell *cell = [ByBetRecordCell cellWithTableView:tableView];
    ByNetModel *netModel = [self.dataArray objectAtIndex:indexPath.row];
    cell.netModel = netModel;
    cell.rightBlock = ^(){
        
        //未支付 去支付
        if (netModel.pay_status.integerValue == 0){
           
            
            self.orderModel = [netModel copy];
            self.payBombView.moneyLabel.text = [NSString stringWithFormat:@"%@元",netModel.money];
            self.orderModel.amount = netModel.money;
            self.payBombView.orderModel = self.orderModel;
            [self.payBombView show];
            
            
        }else{
            
            if (![BY_APPDELEGATE.supportTypeString containsString:[NSString stringWithFormat:@"%@", netModel.gamecate]]) {
                [ByCoreToolAlert showOnlyText:@"暂不支持"];
                return ;
            }
            //大乐透
            if (netModel.gamecate.integerValue == 2001) {
                BySuperLottoVC *vc = [[BySuperLottoVC alloc] init];
                [self by_normalPush:vc hasNavi:YES];
                
             //双色球
            }else if (netModel.gamecate.integerValue == 1001){
                ByDoubleBallVC *vc = [[ByDoubleBallVC alloc] init];
                [self by_normalPush:vc hasNavi:YES];
            
            //足球
            }else if (netModel.gamecate.integerValue == 3010){
                
                ByFootBallBetVC *vc = [[ByFootBallBetVC alloc] init];
                [self by_normalPush:vc hasNavi:YES];
                
            }else if (netModel.gamecate.integerValue == 3001){
                
                ByBasketBallBetVC *vc = [[ByBasketBallBetVC alloc] init];
                [self by_normalPush:vc hasNavi:YES];
                
            }
            
            
            
        }
       
    };
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 75 + 43 + 10;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ByNetModel *netModel = [self.dataArray objectAtIndex:indexPath.row];
    
    //数字彩
    if (netModel.gamecate.integerValue == 2001 || netModel.gamecate.integerValue == 1001) {
        
        ByBetRecordDetailVC *vc = [[ByBetRecordDetailVC alloc] init];
        vc.netModel = netModel;
        [self by_normalPush:vc hasNavi:YES];
        
    }else{
 
        ByBallBetDetailVC *vc = [[ByBallBetDetailVC alloc] init];
        vc.netModel = netModel;
        [self by_normalPush:vc hasNavi:YES];

    }
    
  
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
